﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for ManageEmailMarketing
/// </summary>
public class ManageEmailMarketing
{
	private Up1VNDataContext db;
    public ManageEmailMarketing()
    {
        db = new Up1VNDataContext();
    }
    public List< EmailTemplate> GetEmailTemplate()
    {
        return (from o in db.EmailTemplates
                     select o).ToList();
    }
    public List<EmailPackage> GetEmailPackage()
    {
        return (from o in db.EmailPackages
                     select o).ToList();
    }
    public string GetEmailContentById(int Id)
    {
        var query= (from ep in db.EmailTemplates
                where ep.Id == Id
                select ep).ToList();
        if (query.Count > 0)
            return query[0].EmailTemplateContent;
        else
            return string.Empty;
    }
    public void UpdateEmailTemplate(int EmailTemplateId, int EmailPackageId, int UserId)
    {
        EmailBuyDetail _EmailBuyDetail = new EmailBuyDetail();
        _EmailBuyDetail.EmailPackageId = EmailPackageId;
        _EmailBuyDetail.EmailTemplateId = EmailTemplateId;
        _EmailBuyDetail.DateBuy = DateTime.Now;
        _EmailBuyDetail.UserId = UserId;
        db.EmailBuyDetails.InsertOnSubmit(_EmailBuyDetail);
        db.SubmitChanges();
    }
    public List<IEmailMarketings> GetAllEmailPackageBought(int UserId)
    {
        return (from ebd in db.EmailBuyDetails
                join ep in db.EmailPackages on ebd.EmailPackageId equals ep.Id
                join et in db.EmailTemplates on ebd.EmailTemplateId equals et.Id
                select new IEmailMarketings
                {
                    Point = ep.Point,
                    EmailTemplateName = et.EmailTemplateName,
                    StartDate = ep.StartDate,
                    EndDate = ep.EndDate,
                    Status = ebd.Status,
                    TotalRead = (from em in db.EmailMarketings
                                 where em.EmailBuyDetailId == ebd.Id && em.IsRead == true
                                 group em by em.EmailBuyDetailId into c
                                 select c.Count()).SingleOrDefault().ToString() ?? "0"
                }).ToList();

    }
    public List<IEmailMarketings> GetEmailPackageToSendMail(int UserId)
    {
        return (from ebd in db.EmailBuyDetails
                join ep in db.EmailPackages on ebd.EmailPackageId equals ep.Id
                where ebd.UserId==UserId && ebd.Status==1 && ep.StartDate <= DateTime.Now && DateTime.Now <=ep.EndDate
                select new IEmailMarketings
                {
                    EmailPackageName =string.Format("{0}({1})", ep.Point.ToString(), ebd.DateBuy.Value.ToShortDateString()),
                    BuyEmailDetailId = ebd.Id
                }).ToList();
        
    }
    public bool SendMail(int SenderId, string EmailTitle, int EmailTemplateId, string EmailContent, int user_id)
    {
        try
        {
            EmailMarketing _EmailMarketing = new EmailMarketing();
            _EmailMarketing.Id = Guid.NewGuid();
            _EmailMarketing.SenderId = user_id;
            _EmailMarketing.ReceiverId = SenderId;
            _EmailMarketing.EmailTitle = EmailTitle;
            _EmailMarketing.EmailBuyDetailId = EmailTemplateId;
            _EmailMarketing.EmailContent = EmailContent;
            _EmailMarketing.EmailDate = DateTime.Now;
            db.EmailMarketings.InsertOnSubmit(_EmailMarketing);
            db.SubmitChanges();
            return true;
        }
        catch (Exception)
        {
            return false;
        }
    }
    public int GetTotalMailSender(int EmailPackageId)
    {
        var query = (from ebd in db.EmailBuyDetails
                     join em in db.EmailMarketings on ebd.Id equals em.EmailBuyDetailId
                     where ebd.Id == EmailPackageId
                     select ebd).ToList();
        return query.Count;
    }
    public List<IEmailMarketings> GetPointAndPointMinus(int EmailBuyDetailId)
    {
        return (from ep in db.EmailPackages
                join ebd in db.EmailBuyDetails on ep.Id equals ebd.EmailPackageId
                where ebd.Id == EmailBuyDetailId
                select new IEmailMarketings
                {
                    Point = ep.Point,
                    PointMinus = ep.PointMinus
                }).ToList();
    }
    public List<IEmailMarketings> GetReceiveMail(int userId)
    {
        return (from em in db.EmailMarketings
                join us in db.Users on em.ReceiverId equals us.Users_Id
                where em.ReceiverId == userId && em.IsDetele==false
                select new IEmailMarketings
                {
                   Id= em.Id,
                   EmailTitle=  em.EmailTitle,
                   EmailDate= em.EmailDate,
                   UserEmail = us.Users_Name
                }).ToList();
    }
    public List<IEmailMarketings> GetSendMail(int userId)
    {
        return (from em in db.EmailMarketings
                join us in db.Users on em.SenderId equals us.Users_Id
                where em.SenderId == userId && em.IsDetele == false
                select new IEmailMarketings
                {
                    Id = em.Id,
                    EmailTitle = em.EmailTitle,
                    EmailDate = em.EmailDate,
                    UserEmail = us.Users_Name
                }).ToList();
    }
    public bool DeleteMail(Guid Id)
    {
        try
        {
            EmailMarketing _EmailMarketing = db.EmailMarketings.Single(s => s.Id == Id);
            _EmailMarketing.IsDetele = true;
            db.SubmitChanges();
            return true;
        }
        catch
        {
            return false;
        }
    }
    public IEmailMarketings GetEmailById(Guid Id)
    {
        return (from em in db.EmailMarketings
                join ebd in db.EmailBuyDetails on em.EmailBuyDetailId equals ebd.Id
                join ep in db.EmailPackages on ebd.EmailPackageId equals ep.Id
                join us in db.Users on em.SenderId equals us.Users_Id
                where em.Id == Id && em.IsDetele==false
                select new IEmailMarketings
        {
            Id = em.Id,
            EmailTitle = em.EmailTitle,
            EmailDate = em.EmailDate,
            EmailContent = em.EmailContent,
            UserEmail =us.Users_Name,
            PointPlusUser = ep.PointPlusUser
        }).SingleOrDefault();
    }
    public bool SetMailAlreadyRead(Guid Id)
    {
        try
        {
            EmailMarketing _EmailMarketing = db.EmailMarketings.Single(s => s.Id == Id);
            _EmailMarketing.IsRead = true;
            db.SubmitChanges();
            return true;
        }
        catch
        {
            return false;
        }
    }
    public bool PlusPointUser(int User_Id, int Point)
    {
        try
        {
            User _User = db.Users.Single(s => s.Users_Id == User_Id);
            _User.Users_Point = _User.Users_Point + Point;
            db.SubmitChanges();
            return true;
        }
        catch
        {
            return false;
        }
    }

   
}